﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WaterRevenueMS.WebUI.Common;
using System.ComponentModel.Composition;
using WaterRevenueMS.Core.Service.BasicSet;
using WaterRevenueMS.WebUI.Extension.Filters;
using WaterRevenueMS.Domain.Common.Models;
using WaterRevenueMS.Domain.ViewModels.BasicSet.WaterCaliber;
using WaterRevenueMS.Componnent.Common;
using WaterRevenueMS.Componnent.Entities.BasicSet;
using System.Linq.Expressions;
using WaterRevenueMS.WebUI.Extension.MvcHelper;
namespace WaterRevenueMS.WebUI.Controllers
{

    [Export]
    [PartCreationPolicy(CreationPolicy.NonShared)]
    public class WaterCaliberController : AdminController
    {
        [Import]
        private IWaterCaliberService WaterCaliberService { get; set; }
        [AdminLayoutAttribute]
        public ActionResult Index()
        {
            var model = new WaterCaliberModel();
            return View(model);
        }
        [AdminPermission(PermissionCustomMode.Ignore)]
        public ActionResult List(DataTableParameter param)
        {
            int total = WaterCaliberService.WaterCalibers.Count(t => t.IsDeleted == false);

            var expr = BuildSearchCriteria();

            var filterResult = WaterCaliberService.WaterCalibers.Where(expr).Select(t => new WaterCaliberModel
            {
                Id = t.Id,
                OrderNo = t.OrderNo,
                Shui_BKJ=t.Shui_BKJ,
                Fan_BZDZ=t.Fan_BZDZ,
                Bao_JZDZ=t.Bao_JZDZ,
                Bao_XFY=t.Bao_XFY,
                Chai_BFY=t.Chai_BFY

            }).OrderBy(t => t.OrderNo).Skip(param.iDisplayStart).Take(param.iDisplayLength).ToList();

            int sortId = param.iDisplayStart + 1;

            var result = from c in filterResult
                         select new[]
                        {
                            sortId++.ToString(), 
                            c.Shui_BKJ,
                            c.Fan_BZDZ.ToString(),
                            c.Bao_JZDZ.ToString(),
                            c.Bao_XFY.ToString(),
                            c.Chai_BFY.ToString(),
                            c.OrderNo.ToString(),
                            c.Id.ToString()
                        };

            return Json(new
            {
                sEcho = param.sEcho,
                iDisplayStart = param.iDisplayStart,
                iTotalRecords = total,
                iTotalDisplayRecords = total,
                aaData = result
            }, JsonRequestBehavior.AllowGet);
        }

        public ActionResult Create()
        {
            var model = new WaterCaliberModel();

            return PartialView(model);
        }

        [HttpPost]
        [AdminOperateLog]
        public ActionResult Create(WaterCaliberModel model)
        {
            if (ModelState.IsValid)
            {
                OperationResult result = WaterCaliberService.Insert(model);
                if (result.ResultType == OperationResultType.Success)
                {
                    return Json(result);
                }
                else
                {
                    return PartialView(model);
                }
            }
            else
            {
                return PartialView(model);
            }
        }

        public ActionResult Edit(int Id)
        {
            var model = new WaterCaliberModel();
            var entity = WaterCaliberService.WaterCalibers.FirstOrDefault(t => t.Id == Id);
            if (null != entity)
            {
                model = new WaterCaliberModel
                {
                    Shui_BKJ = entity.Shui_BKJ,
                    DefaultValue = model.DefaultValue,
                    OrderNo = entity.OrderNo,
                    Fan_BZDZ = entity.Fan_BZDZ,
                    Bao_JZDZ = entity.Bao_JZDZ,
                    Bao_XFY = entity.Bao_XFY,
                    Chai_BFY = entity.Chai_BFY
                };
            }
            return PartialView(model);
        }

        [HttpPost]
        public ActionResult Edit(WaterCaliberModel model)
        {
            if (ModelState.IsValid)
            {
                OperationResult result = WaterCaliberService.Update(model);
                if (result.ResultType == OperationResultType.Success)
                {
                    return Json(result);
                }
                else
                {
                    return PartialView(model);
                }

            }
            else
            {
                return PartialView(model);
            }
        }


        public ActionResult Delete(int Id)
        {
            OperationResult result = WaterCaliberService.Delete(Id);
            return Json(result);
        }


        private Expression<Func<WaterCaliber, Boolean>> BuildSearchCriteria()
        {
            DynamicLambda<WaterCaliber> bulider = new DynamicLambda<WaterCaliber>();
            Expression<Func<WaterCaliber, Boolean>> expr = null;
            if (!string.IsNullOrEmpty(Request["Shui_BKJ"]))
            {
                var data = Request["Shui_BKJ"].Trim();
                Expression<Func<WaterCaliber, Boolean>> tmp = t => t.Shui_BKJ.Contains(data);
                expr = bulider.BuildQueryAnd(expr, tmp);
            }
            Expression<Func<WaterCaliber, Boolean>> tmpSolid = t => t.IsDeleted == false;
            expr = bulider.BuildQueryAnd(expr, tmpSolid);

            return expr;
        }
    }
}
  